/*
 * Created on 20/11/2007
 *
 */
package org.seginfo.httpfuzzer;

import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;
import java.util.Observable;

import org.seginfo.httpfuzzer.connection.FuzzHttpRequest;

/**
 * @author Matu
 */
public class CaseRunner extends Observable implements Runnable {
	
	private URL url;
	private FuzzCase fuzzCase;
	private CaseLogger caseLogger;
	
	/**
	 * 
	 */
	public CaseRunner(URL url, FuzzCase fuzzCase, CaseLogger caseLogger) {
		super();
		this.url = url;
		this.fuzzCase = fuzzCase;
		this.caseLogger = caseLogger;
	}

	/**
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		
		/* Run FuzzCase */
		FuzzHttpRequest request = new FuzzHttpRequest(url, fuzzCase);
		try {
			request.connect();
		} catch (ConnectException e) {
			this.fuzzCase.setResult(FuzzCase.CONNECTION_REFUSED);
		} catch (IOException e) {
			this.fuzzCase.setResult(FuzzCase.CONNECTION_RESET);
		} catch (RuntimeException e) {
			this.fuzzCase.setResult(FuzzCase.NO_RESPONSE);
		}
		this.fuzzCase.setHost(url.getHost());
		this.fuzzCase.setServer(request.getServer());
		this.fuzzCase.setStatus(request.getStatus());
		this.caseLogger.logCase(fuzzCase);
		
		/* FuzzCase finished notification */
		this.setChanged();
		this.notifyObservers(fuzzCase);
	}
}
